﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;

namespace Aioms.Controllers
{
    [DynamicApiController]
    public class RoleEmployeeController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public RoleEmployeeController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 用户ID列表
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostList([FromForm] long eid, [FromForm] long rid)
        {
            List<long> epids = await db.Queryable<RoleEmployee>().Where(ru => ru.Eid == eid && ru.Rid == rid).Select(ru => ru.Epid).ToListAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = epids,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <param name="epids"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostSave([FromForm] long eid, [FromForm] long rid, [FromForm] long[] epids)
        {
            // 先清空之前该用户
            await db.Deleteable<RoleEmployee>().Where(ru => ru.Eid == eid && ru.Rid == rid).ExecuteCommandAsync();

            List<RoleEmployee> roleusers = new();
            foreach (long epid in epids)
            {
                roleusers.Add(new RoleEmployee { Rid = rid, Epid = epid });
            }
            await db.Insertable<RoleEmployee>(roleusers).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="rid"></param>
        /// <param name="epid"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostDelete([FromForm] long eid, [FromForm] long rid, [FromForm] long epid) {
            int affrows = await db.Deleteable<RoleEmployee>().Where(ru => ru.Eid == eid && ru.Rid == rid && ru.Epid == epid).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
